package com.app.twth.ui.adjust.lpn;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.app.twth.BR;
import com.app.twth.R;
import com.app.twth.app.AppRouter;
import com.app.twth.base.BaseActivity;
import com.app.twth.databinding.ActivityAdjustSubmitBinding;
import com.app.twth.ui.adjust.AdjustViewModel;
import com.app.twth.utils.ToastUtil;
import com.app.twth.utils.Utils;

/**
 * @describe :
 */
public class AdjustSubmitActivity extends BaseActivity<ActivityAdjustSubmitBinding, AdjustViewModel> {
    private String TAG = getClass().getSimpleName();
    private AdjustLpnQr adjustLpnQr;

    @Override
    protected void initListener() {
        viewModel.submitLiveData.observe(this, data -> {
            Intent intent = new Intent();
            setResult(RESULT_OK, intent);
            finish();
        });

        viewModel.errorResultCode.observe(this, code -> {
            showHint(code);
        });
        viewModel.isLoading.observe(this, b -> {
            if (b.isShow()) {
                showProgressDialog(b.getMsg());
            } else {
                dismissProgressDialog();
            }
        });

    }

    protected String setTitle() {
        return "Adjust(Submit)";
    }

    @Override
    protected int initViewModelId() {
        return BR.viewModel;
    }

    @Override
    protected void initData() {
        setNext(getString(R.string.submit));
        Bundle bundle = getIntent().getBundleExtra(AppRouter.K_EXTRA);
        adjustLpnQr = (AdjustLpnQr) bundle.getSerializable("data");
        binding.tvLpn.setText(adjustLpnQr.getIbLpn());
        binding.tvSku.setText(adjustLpnQr.getSku() + "(" + adjustLpnQr.getQty() + ")");

        binding.tvCode.requestFocus();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        bottomLayoutBinding.tvHint.setText("");
        initScan();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        unScan();
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_scan:
                setScan();
                break;
            case R.id.tv_reset:
                binding.tvCode.setText("");
                break;
            case R.id.tv_next:
                if (System.currentTimeMillis() - timeMillis < spaceTime) {//两秒内触发一次
                    return;
                }
                timeMillis = System.currentTimeMillis();
                String location = binding.tvCode.getText().toString();
                if (Utils.isEmpty(location)) {
                    ToastUtil.showToast("Please enter location");
                    return;
                }
                adjustLpnQr.setLocationCode(location);
                viewModel.adjustInfo(adjustLpnQr);
                break;
        }
    }

    @Override
    public void getScanninCode() {
        super.getScanninCode();
        binding.tvCode.setText(barcodeStr);
        bottomLayoutBinding.tvHint.setText("");
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
